﻿using Infrastructures;
using IRepository;
using Models;
using Spring.Data.Common;
using Spring.Data.Generic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryMySQL
{
    public class AgentRepository : IAgentRepository
    {
        private string baseAgent = "select AgentNO,Name,SchoolNO,Resume,LogoPath,Grade,CreateTime,UpdateTime,Status,Remark,AgentPassword from Agent";
        public Agent Create(Agent obj)
        {
            string strIns = @"insert into Agent(AgentNO,Name,SchoolNO,Resume,LogoPath,Grade,CreateTime,UpdateTime,Status,Remark,AgentPassword) values(@AgentNO,@Name,@SchoolNO,@Resume,@LogoPath,@Grade,@CreateTime,@UpdateTime,@Status,@Remark,@AgentPassword);";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();

            p.Add("AgentNO", DbType.Int32).Value = obj.AgentNO;
            p.Add("Name", DbType.String).Value = obj.Name;
            p.Add("SchoolNO", DbType.Int32).Value = obj.SchoolNO;
            p.Add("Resume", DbType.String).Value = obj.Resume;
            p.Add("LogoPath", DbType.String).Value = obj.LogoPath;
            p.Add("Grade", DbType.String).Value = obj.Grade;
            p.Add("CreateTime", DbType.DateTime).Value = obj.CreateTime;
            p.Add("UpdateTime", DbType.DateTime).Value = obj.UpdateTime;
            p.Add("Status", DbType.String).Value = obj.Status;
            p.Add("Remark", DbType.String).Value = obj.Remark;
            p.Add("AgentPassword", DbType.String).Value = obj.AgentPassword;

            DBHelper.TemplateMySQL.ExecuteNonQuery(CommandType.Text, strIns, p);
            return obj;
        }
        public void Update(Agent obj)
        {
            string strUpdate = @"update Agent set Name=@Name,SchoolNO=@SchoolNO,Resume=@Resume,LogoPath=@LogoPath,Grade=@Grade,CreateTime=@CreateTime,UpdateTime=@UpdateTime,Status=@Status,Remark=@Remark,AgentPassword=@AgentPassword where AgentNO=@AgentNO";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();

            p.Add("AgentNO", DbType.Int32).Value = obj.AgentNO;
            p.Add("Name", DbType.String).Value = obj.Name;
            p.Add("SchoolNO", DbType.Int32).Value = obj.SchoolNO;
            p.Add("Resume", DbType.String).Value = obj.Resume;
            p.Add("LogoPath", DbType.String).Value = obj.LogoPath;
            p.Add("Grade", DbType.String).Value = obj.Grade;
            p.Add("CreateTime", DbType.DateTime).Value = obj.CreateTime;
            p.Add("UpdateTime", DbType.DateTime).Value = obj.UpdateTime;
            p.Add("Status", DbType.String).Value = obj.Status;
            p.Add("Remark", DbType.String).Value = obj.Remark;
            p.Add("AgentPassword", DbType.String).Value = obj.AgentPassword;

            DBHelper.TemplateMySQL.ExecuteNonQuery(CommandType.Text, strUpdate, p);
        }
        public Agent Get(string code)
        {
            string strSel = baseAgent + " where AgentNO=@AgentNO";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();
            p.Add("AgentNO", DbType.Int32).Value = code.ToInt();


            IList<Agent> lst = DBHelper.TemplateMySQL.QueryWithRowMapperDelegate(CommandType.Text, strSel, new RowMapperDelegate<Agent>(MapAgent), p);
            return lst.Count == 0 ? null : lst[0];
        }
        public IList<Agent> ListAgent()
        {
            string strSel = baseAgent;

            IList<Agent> lst = DBHelper.TemplateMySQL.QueryWithRowMapperDelegate(CommandType.Text, strSel, new RowMapperDelegate<Agent>(MapAgent));

            return lst;
        }

        public int GetAgentID(string agentPassword)
        {
            string strSel = "select AgentNO from Agent where AgentPassword=@AgentPassword";

            IDbParameters p = DBHelper.TemplateMySQL.CreateDbParameters();
            p.Add("AgentPassword", DbType.String).Value = agentPassword;

            object obj = DBHelper.TemplateMySQL.ExecuteScalar(CommandType.Text, strSel, p);

            return obj == null ? 0 : obj.ToString().ToInt();
        }

        private Agent MapAgent(IDataReader dr, int count)
        {
            Agent obj = new Agent();
            obj.AgentNO = dr.GetInt32(0);
            obj.Name = dr.IsDBNull(1) ? String.Empty : dr.GetString(1);
            obj.SchoolNO = dr.GetInt32(2);
            obj.Resume = dr.IsDBNull(3) ? String.Empty : dr.GetString(3);
            obj.LogoPath = dr.IsDBNull(4) ? String.Empty : dr.GetString(4);
            obj.Grade = dr.IsDBNull(5) ? String.Empty : dr.GetString(5);
            obj.CreateTime = dr.GetDateTime(6);
            obj.UpdateTime = dr.GetDateTime(7);
            obj.Status = (CommonStatus)Enum.Parse(typeof(CommonStatus), dr.GetString(8), true);
            obj.Remark = dr.IsDBNull(9) ? String.Empty : dr.GetString(9);
            obj.AgentPassword = dr.IsDBNull(10) ? String.Empty : dr.GetString(10);

            return obj;
        }
    }
}
